package apj.demo.table.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import apj.demo.factory.ServiceDaoFactory;
import apj.demo.model.Bill;
import apj.demo.model.Department;
import apj.demo.model.Vendor;

/**
 * 账单添加操作后台操作方法
 */
@WebServlet("/do.bill")
public class BillAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String method = request.getParameter("method");
		if(method != null){
			switch(method){
			case "0":
				try {
					frist_show(request,response);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case "1":
				try {
					second_show(request,response);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case "2":
				try {
					save(request,response);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
		}
	}


	private void save(HttpServletRequest request, HttpServletResponse response) throws ParseException, SQLException {
		//获取参数
		String bill_date = request.getParameter("bill_date");
		String bill_due_date = request.getParameter("bill_due_date");
		String vendor = request.getParameter("vendor");
		String sum = request.getParameter("sum");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new java.sql.Date(dateFormat.parse(bill_date).getTime());
		Date due_date = new java.sql.Date(dateFormat.parse(bill_due_date).getTime());
		
		//数据封装
		Bill bill = new Bill();
		bill.setBillPay(Double.parseDouble(sum));
		bill.setBillDate(date);
		bill.setBillDueDate(due_date);
		bill.setVendor(vendor);
		bill.setBillFinish(false);
		bill.setPayType("wai");
		
		ServiceDaoFactory.getBIllServiceDaoImpl().save(bill);
	}


	private void second_show(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Department> deps = ServiceDaoFactory.getDepartmentServiceDaoImpl().findByAll();
		List<String> deps_name = new ArrayList<String>();
		for(Department dep:deps){
			deps_name.add(dep.getDepName());
		}
		com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
		json.put("deps_name", deps_name);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
		
	}


	/*点击功能菜单账单管理，显示页面*/
	private void frist_show(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		//获取所有供货商的
		List<Vendor> vendors = ServiceDaoFactory.getVendorServiceDaoImpl().findByAll();
		List<String> ven_name = new ArrayList<String>();
		for(Vendor v:vendors){
			ven_name.add(v.getVendor());
		}
		com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
		json.put("vendors", ven_name);
		PrintWriter out = response.getWriter();
		out.print(json);
		out.close();
	}

}
